package com.example.demo.mybatis.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.demo.common.domain.CommonResult;
import com.example.demo.mybatis.domain.TUser;
import com.example.demo.mybatis.service.IAccountService;
import com.example.demo.mybatis.service.ICustService;
import com.example.demo.mybatis.service.IUserService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "1 用户管理")
@RestController
@RequestMapping("/mp/user")
public class MPUserController {
    private static final Logger logger = LoggerFactory.getLogger(MPUserController.class);

    @Autowired
    private IUserService userService;
    @Autowired
    private ICustService custService;
    @Autowired
    private IAccountService accountService;

    @ApiOperation(value = "测试接口hi")
    @GetMapping("/hi")
    public CommonResult hi(){
    	logger.debug("debug");
    	logger.info("info");
    	logger.warn("warn");
    	logger.error("error");
        return CommonResult.ok();
    }

    @ApiOperation(value = "用户列表")
    @GetMapping("")
    public CommonResult testSelect() {
        System.out.println(("----- selectAll method test ------"));
        List<TUser> userList = userService.list();
        userList.forEach(System.out::println);
        return CommonResult.ok(userList);
    }

    @ApiOperation(value = "用户查询", notes = "根据url的id查询对象")
    @GetMapping("/{id}")
    public CommonResult testSelect(@PathVariable Long id) {
        return CommonResult.ok(userService.getById(id));
    }

    @ApiOperation(value = "新增用户")
    @PostMapping("/add")
    public CommonResult testInsert(@Valid @RequestBody TUser user) {
        if(user == null){
            user = new TUser();
            user.setAge(18);
            user.setName("hehe");
            user.setEmail("33@qq.com");
            user.setId(100l);
            user.setCtime(new Date());
        }
        System.out.println(user.getCtime());
        userService.save(user);
        return CommonResult.ok();
    }

    @GetMapping("/update")
    public Object testUpdate() {
        TUser user = userService.getById(100);
        user.setAge((new Random()).nextInt(50));
        userService.updateById(user);
        return "ok";
    }

    @GetMapping("/delete")
    public Object testDelete() {
        TUser user = userService.getById(100);
        userService.removeById(user);
        return "ok";
    }




    @GetMapping("/ds")
    public Object testDyDS(){
        userService.list();
        custService.list();
        accountService.list();
        return "ok";
    }


    @GetMapping("/badd")
    public Object testBatchInsert() {
        List<TUser> list = new ArrayList<>();
        TUser user = null;
        for (int i = 1; i <= 20; i++) {
            user = new TUser();
            user.setId(100l+i);
            user.setAge(18);
            user.setName("hehe"+i);
            user.setEmail((100+i)+"@qq.com");
            list.add(user);
        }
        //一次性插入
//        userService.saveBatch(list);
        //分批插入
        boolean b = userService.saveBatch(list, 5);
        System.out.println(b);

        user = new TUser();
        user.setId(1000l);
        user.setAge(18);
        user.setName("hehe1000");
        user.setEmail((1000)+"@qq.com");
        //单个对象插入
        userService.save(user);
        return "ok";
    }

    @GetMapping("/w")
    public void wrapperOne() {
//        QueryWrapper<User> wrapper = new QueryWrapper();
        QueryWrapper<TUser> wrapper = Wrappers.query();
        wrapper.select("name","age").like("name", "J").lt("age", 20);
        List<TUser> users = userService.list(wrapper);
        users.forEach(System.out::println);

//        UpdateWrapper updateWrapper = new UpdateWrapper();
//        UpdateWrapper updateWrapper = Wrappers.update();

    }

    @GetMapping("/getByName/{name}")
    public CommonResult jdbcTemplate(@PathVariable String name){
        return CommonResult.ok(userService.jdbcTemplateTest(name));
    }
    @GetMapping("/getByAge/{age}")
    public CommonResult jdbcTemplate(@PathVariable Integer age){
        return CommonResult.ok(userService.findByAge(age));
    }
    @GetMapping("/uupdate/{age}")
    public CommonResult transactionalTest(@PathVariable Integer age){
        TUser user1 = userService.getById(1);
        TUser user2 = userService.getById(2);
        user1.setAge(30);
        user2.setAge(age);
        try {
            userService.updateUpdate(user1, user2);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return CommonResult.ok();
    }
}
